package com.dayuanit.dy17.library.service.impl;

import com.dayuanit.dy17.library.entity.Author;
import com.dayuanit.dy17.library.exception.BizException;
import com.dayuanit.dy17.library.mapper.AuthorMapper;
import com.dayuanit.dy17.library.mapper.BookMapper;
import com.dayuanit.dy17.library.service.IAuthorService;
import com.dayuanit.dy17.library.util.DBUtils;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
public class AuthorServiceImpl implements IAuthorService {

    @Autowired
    private AuthorMapper authorMapper;

    @Autowired
    private BookMapper bookMapper;

    public AuthorServiceImpl() {
        System.out.println("-------AuthorServiceImpl------");
    }

    @Override
    public Author queryAuthor(String authorName) {
        Author author = authorMapper.getAuthor(authorName);
        if (null == author) {
            throw new BizException("作者不存在");
        }
        return author;
    }


    @Transactional
    @Override
    public void changeAuthorName(String oldName, String newName) {
        int rows = authorMapper.modifyAuhtorName(oldName, newName);
//        int i = 9 / 0;
        bookMapper.modifyAuthorName(oldName, newName);
    }

}
